package com.ivt.controller.console;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.ivt.core.util.Constants;
import com.ivt.model.Classes;
import com.ivt.model.view.ClassesView;
import com.ivt.model.view.QueryCriteria;
import com.ivt.model.view.Rst;
import com.ivt.service.ClassesService;
import com.ivt.utils.Convert;

@Controller
@RequestMapping("/console/classes")
public class ClassesController {

	@Resource
	private ClassesService classesService;

	@RequestMapping("/create")
	public String create() {

		return "alone:console.classes-create";
	}

	@RequestMapping("/list")
	public String list(ModelMap view) {
		return "alone:console.classes-list";
	}

	@RequestMapping("/modify/{id}")
	public String modify(@PathVariable Long id, ModelMap view) {

		Classes classes = classesService.getClasses(id);

		view.addAttribute("row", classes);

		return "alone:console.classes-modify";
	}

	@RequestMapping("/save")
	@ResponseBody
	public Rst save(Classes classes) {

		Rst rst = new Rst();
		classes = classesService.create(classes);
		if (0 < classes.getClassesId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/pagination")
	@ResponseBody
	public Rst pagination(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		List<Classes> list = classesService
				.queryClassesByCriteria(queryCriteria);
		List<ClassesView> views = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty(list)) {
			for (Classes classes : list) {
				views.add(Convert.toClassesView(classes));
			}
		}
		rst.setCode(Constants.SUCCESS);
		rst.setData(views);
		return rst;
	}

	@RequestMapping("/pages")
	@ResponseBody
	public Rst pages(QueryCriteria queryCriteria) {

		Rst rst = new Rst();
		long count = classesService.queryCount(queryCriteria);
		rst.setCode(Constants.SUCCESS);
		rst.setData(count);
		return rst;
	}

	@RequestMapping("/update")
	@ResponseBody
	public Rst update(Classes classes) {
		Rst rst = new Rst();
		classes = classesService.update(classes);
		if (0 < classes.getClassesId()) {
			rst.setCode(Constants.SUCCESS);
			rst.setMessage("操作成功");
		}
		return rst;
	}

	@RequestMapping("/del")
	@ResponseBody
	public Rst update(Long[] ids) {
		Rst rst = new Rst();
		int c = classesService.deletes(ids);
		rst.setCode(Constants.SUCCESS);
		rst.setMessage("操作成功");
		rst.setData(c);
		return rst;
	}
}
